﻿/*
   Open Intel 
   Copyright © 2011 – ISC. All Rights Reserved.
*/
using System.ComponentModel;
using System.Windows.Controls;
using Caliburn.Micro;
using OI.Events;
using OI.Framework;

namespace OI.Controls
{
    public partial class Toolbox : INotifyPropertyChanged
    {
        public BindableCollection<ToolboxItem> Items
        {
            get
            {
                return _Items;
            }
            set
            {
                _Items = value;
                OnPropertyChanged("Items");
            }
        }
        private BindableCollection<ToolboxItem> _Items = new BindableCollection<ToolboxItem>();


        public Toolbox()
        {
            InitializeComponent();
            LayoutRoot.DataContext = this;
        }


        // called when an item is checked
        private void ItemChecked(object sender, System.Windows.RoutedEventArgs e)
        {
            var rb = (RadioButton)sender;
            string key = rb.Tag as string;

            if (!string.IsNullOrEmpty(key))
            {
                var ea = IoC.Get<IEventAggregator>();
                ea.Publish(new ChooseToolboxItem
                {
                    Key = key,
                    Group = rb.GroupName
                });
            }
        }


        public void DeselectAll()
        {
            Items.Apply((x)=>x.IsSelected = false);
            Items.NotifyOfPropertyChange(string.Empty);
        }


        #region INotifyPropertyChanged Members

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;


        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="property">The property.</param>
        public void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
        #endregion
    }
}
